data Square = Empty | Black | White deriving Eq
instance Show Square where
   show Empty = "[ ]"
   show Black = "[B]"
   show White = "[W]"
data Turn = BlackTurn | WhiteTurn deriving (Show,Eq)

--Functions for displaying the game board
printBoard :: [[Square]] -> IO()
printBoard board = do
    putStrLn $ printhelp board
    return()

printhelp :: [[Square]] -> String
printhelp [] = ""
printhelp (x:xs) = (printLine x) ++ "\n" ++(printhelp xs)

printLine :: [Square] -> String
printLine [] = ""
printLine (x:xs) = show x ++ " " ++ (printLine xs)

--Functions for starting the game
startboard height turn = [[chooseSquare height x y | x <- [1..height]] | y<-[1..height]]

chooseSquare height x y
    | (height/2) == x && (height/2) == y = Black
    | (height/2)+1 == x && (height/2)+1 == y = Black 
    | (height/2) == x && (height/2)+1 == y = White
    | (height/2)+1 == x && (height/2) == y = White
    | otherwise = Empty

  
--Old, returns bools instead of list of coords
{-
isCapture x y dx dy board turn
	| (x >=(length board)-1) || (y>=(length board)-1)= False  
	| ((board !! x) !! y) /= Empty = False
	| ((board !! (x+dx)) !! (y+dy)) == turn = False 
	| otherwise = isCapture' (x) (y) dx dy board turn	

isCapture' x y dx dy board turn
	| (x >=(length board)-1) || (y>=(length board)-1)= False  
	| ((board !! (x+dx)) !! (y+dy)) == turn = True
	| ((board !! (x+dx)) !! (y+dy)) == Empty = False
	| otherwise = isCapture' (x+dx) (y+dy) dx dy board turn 
-}



--Functions for determining if a move is proper, or if a square should be flipped
isCapture x y dx dy board turn
  | (x >=(length board)-1) || (y>=(length board)-1) = []
  | ((board !! x) !! y) /= Empty = []
  | ((board !! (x+dx)) !! (y+dy)) == turn = []
  | otherwise = isCapture' (x) (y) dx dy board turn

isCapture' x y dx dy board turn
  | (x>=(length board)-1) || (y>=(length board)-1) = []
	| ((board !! (x+dx)) !! (y+dy)) == turn = [(x,y)]
  | ((board !! (x+dx)) !! (y+dy)) == Empty = []
  | otherwise = isCapture' (x+dx) (y+dy) dx dy board turn


--Functions that are in the move-making process
toUpdate board turn x y = 
  (isCapture' x y 1 0 board turn)++(isCapture' x y (-1) 0 board turn)++(isCapture' x y 0 1 board turn)++(isCapture' x y 0 (-1)  board turn)++(isCapture' x y 1 1 board turn)++(isCapture' x y 1 (-1) board turn)++(isCapture' x y (-1) 1 board turn)++(isCapture' x y (-1) (-1) board turn)

updateHelper board turn [] = board
updateHelper board turn (x:xs) = updateAt board (fst x) (snd x) turn

updateBoard board turn x y = updateHelper board turn (toUpdate board turn x y)


blackSquares board = [[if (((board !! x) !! y) == Black) then (x,y) else (-1,-1) | x <- [0..(length board)-1]] | y<-[0..(length board)]]

whiteSquares board = [[if (((board !! x) !! y) == White) then (x,y) else (-1,-1) | x <- [0..(length board)]] | y<-[0..(length board)]]

updateBoard board turn x y = [[if (x,y) `elem` (toUpdate board turn x y) then turn else Empty | x <- [1..(length board)-1]] | y<-[1..(length board)-1]]

--only checks top row.
isWon board = if Empty `elem` board !! 1 then False else True
